#ifndef CHUNK_H
#define CHUNK_H

// stl
#include <vector>
// raylib
#include <raylib.h>
// maze
#include <mapbase.h>

class Chunk
{
private:
    Coord<int> coord;
    int seed = 0;
    bool inited;
    std::vector<std::vector<int>> map;
public:
    // constant
    static const int chunksize = 4;
    // shader_depth
    RenderTexture2D texture_shader_depth;
    // test grid
    bool created=false;
    std::map<int,std::vector<Rectangle>> gridMap;
public:
    // Chunk() = delete;
    Chunk() : coord({0, 0}), inited(false), 
              map(chunksize, std::vector<int>(chunksize, 0)),created(true) {}

    Chunk(const Coord<int>& coorda){
        inited = false;
        coord = coorda;
        map = {chunksize, std::vector<int>(chunksize, Blocks::wall)};
        created = true;
        texture_shader_depth = LoadRenderTexture(chunksize*48, chunksize*48);
    }
    Chunk(const Coord<int>& coorda, int seed){
        inited = false;
        coord = coorda;
        // srand(time(NULL));
        created=true;
        map = {chunksize, std::vector<int>(chunksize, 0)};
        for(int i=0; i<chunksize; i++)
            for(int j=0; j<chunksize; j++)
                map[i][j] = rand()%4;
    }

    ~Chunk(){
        map.clear();
        // UnloadTexture(texture_shader_depth.texture);
    }

    std::vector<std::vector<int>> getMap(){
        return map;
    }

    void setMap(std::vector<std::vector<int>> mapa){
        map.clear();
        map = mapa;
    }

    int getBlock(int localX, int localY) const {
        if (localX >= 0 && localX < chunksize && localY >= 0 && localY < chunksize) {
            return map[localX][localY];
        }
        return 0;
    }

    void setBlock(int localX, int localY, int type){
        if (localX >= 0 && localX < chunksize && localY >= 0 && localY < chunksize)
            map[localX][localY] = type;
    }

    Coord<int> getCoord(){
        return coord;
    }

    Vector2 getCoordV(){
        return {(float)coord.x,(float)coord.y};
    }

    // test grid
    std::map<int,std::vector<Rectangle>> getGridMap()
    {
        return gridMap;
    }

    void addBlock(Rectangle rec,int blockType)
    {
        gridMap[blockType].push_back(rec);
    }
};

#endif